package com.java.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.mapper.DepartMapper;
import com.java.mapper.UserMapper;
import com.java.po.Department;
import com.java.po.DepartmentMap;
import com.java.po.User;
import com.java.service.DepartService;
import com.java.tool.AjaxJson;

/**
 * Title:DepartServiceImpl.java
 * Description:
 * @author:sprogrammer
 * @time:2018年2月22日下午10:33:51
 */
@Service("departServiceImpl")
public class DepartServiceImpl implements DepartService {
	
	@Autowired
	private DepartMapper departMapper;
	private AjaxJson ajaxJson;
	
	public DepartServiceImpl(){
		ajaxJson = new AjaxJson();
	}
	
	public void cleanAjaxJson(){
		ajaxJson.setData("");
		ajaxJson.setTotal(0);
		ajaxJson.setMessage("");
	}
	
	@Override
	public AjaxJson insertDepartment(Department department) throws Exception {
		// TODO 自动生成的方法存根
		cleanAjaxJson();
		int flag = departMapper.insertDepartment(department);
		if(flag!=1){
			ajaxJson.setMessage("fault");
			return ajaxJson;
		}
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@SuppressWarnings("resource")
	@Override
	public AjaxJson deleteDepartmentById(Department department) throws Exception {
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		Department d = departMapper.findDepartmentByid(department);
		int flag = departMapper.deleteDepartmentById(department);
		if(flag!=1){
			ajaxJson.setMessage("fault");
			return ajaxJson;
		}
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
		User user = new User();	
		user.setDepartment(d.getDepartname());
		UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");		
		userMapper.updateUserByDepartment(user);
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@Override
	public AjaxJson updateDepartmentById(Department department) throws Exception {
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		int flag = departMapper.updateDepartmentById(department);
		if(flag!=1){
			ajaxJson.setMessage("fault");
			return ajaxJson;
		}
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@Override
	public AjaxJson findAllDepartment(DepartmentMap departmentMap) throws Exception {
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		Department department =new Department();
		int page = departmentMap.getPage();
		int pageSize = departmentMap.getPageSize();
		if(departmentMap.getDepartment()!=null){
			department = departmentMap.getDepartment();
		}
		PageHelper.startPage(page,pageSize);
		List<Department> list = departMapper.findAllDepartment(department);
		PageInfo<Department> pageInfo = new PageInfo<>(list);
		ajaxJson.setData(pageInfo.getList());
		ajaxJson.setTotal((int)pageInfo.getTotal());
		return ajaxJson;
	}

	@Override
	public AjaxJson findDepartmentByid(Department department) throws Exception {
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		department = departMapper.findDepartmentByid(department);
		ajaxJson.setData(department);
		ajaxJson.setMessage("success");
		ajaxJson.setTotal(1);
		return ajaxJson;
	}

}
